<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
			array('op'=>'list','title'=>'活动列表'),
			array('op'=>'edit','title'=>'添加活动'),
);

//会员列表
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'type'=>1);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$list=pdo_getslice('jy_scrm_sale_game',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=array('username'=>$item['username'],'avatar'=>$item['avatar'],'nickname'=>$item['nickname']);
			$item['url']=custom_url('jy_scrm/sale.redlist/detail/id='.$item['id'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：玩法：用户拆裂变红包时，会获得首拆红包金额，并立刻入账至用户的微信零钱里，此时在规定时间内，若分享给好友帮拆，好友可获得金额不等的红包作为奖励，即刻到账。当凑齐了相应的好友人数，用户可以获得剩下的红包。反之，如果用户没有分享领取完的话，裂变的红包剩余金额在超过规定时间后则会失效。。</br>
		在使用派发红包功能前，需要商户完成以下配置：</br>
		1、前往【微信支付商户平台】注册微信支付商户号</br>
		2、登录【企业微信后台】开通企业支付，绑定已有商户号</br>
		3、登录【微信支付商户平台】开通【企业付款到零钱】</br>
		4、在本系统，进入【应用中心】--【企业支付】，点击，填入企业支付的应用ID和Secret</br>
		5、在本系统，进入【应用中心】--【企业支付】，点击，完成支付配置。
		','edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'title','title'=>'活动名称','type'=>'string'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'pcd','title'=>'奖品（剩余/总库存）','type'=>'string'),
			array('name'=>'groupid','title'=>'活动限制','type'=>'select','options'=>'groups'),
			array('name'=>'workerid','title'=>'引流成员','type'=>'select','options'=>'workers','option_title'=>'name'),
			array('type'=>'switch','title'=>'状态','name'=>'status','set'=>'set'),
			array('name'=>'url','title'=>'链接地址','type'=>'copy'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'活动名称','type'=>'string','width'=>'15'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status','width'=>'15'),
			array('type'=>'choose','width'=>15,'title'=>'引流成员','name'=>'mid','action'=>'jy_scrm/worker.get_worker/'),
			array('name'=>'createtime','width'=>15,'title'=>'添加时间','type'=>'time_range'),
		),	
	);
	$return['status']=array('未开始','进行中','已结束');
	$return['workers']=$this->getall('worker',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($_RQ['postdata']){
		$postdata=get_postdata();
		if(!$postdata['title']){
			exi('活动名称必填！','error');
		}
		if(!$postdata['starttime'] || !$postdata['endtime']){
			exi('开始时间，结束时间必填！','error');
		}
		$data=array(
			'endtime'=>strtotime($postdata['endtime']),
			'poster_data'=>ijson_encode($postdata['poster_data']),
		);
		$data['title']=$postdata['title'];
			$data['desc']=$postdata['desc'];
			$data['workerid']=intval($postdata['workerid']);
			$data['starttime']=strtotime($postdata['starttime']);
			$data['endtime']=strtotime($postdata['endtime']);
			$data['need_lauser']=intval($postdata['need_lauser']);
			$data['help_num']=intval($postdata['help_num']);
			$data['prize_sendtype']=intval($postdata['prize_sendtype']);
			$data['prize_type']=intval($postdata['prize_type']);
			$data['prize_title']=$postdata['prize_title'];
			$data['prize_total']=intval($postdata['prize_total']);
			$data['gz_fasttimes']=$postdata['gz_fasttimes'];
			$data['gz_limittimes']=$postdata['gz_limittimes'];
			$data['gz_limitdaytimes']=$postdata['gz_limitdaytimes'];
			$data['gz_limitprizetimes']=$postdata['gz_limitprizetimes'];
			$data['gz_limitdayprizetimes']=$postdata['gz_limitdayprizetimes'];
			$data['gz_shareonetimes']=$postdata['gz_shareonetimes'];
			$data['gz_dayshareaddtimesmax']=$postdata['gz_dayshareaddtimesmax'];		
			$data['red_set']=ijson_encode($postdata['red_set']);
			$data['set_tag']=$postdata['set_tag'];
			$data['get_sextype']=intval($postdata['get_sextype']);
			$data['get_areatype']=intval($postdata['get_areatype']);
			$data['get_areas']=intval($postdata['get_areas']);
			$data['welcome']=$postdata['welcome'];
			$data['share_title']=$postdata['share_title'];
			$data['share_desc']=$postdata['share_desc'];
			$data['share_image']=$postdata['share_image'];
		$id=intval($postdata['id']);
		if($id){
			pdo_update('jy_scrm_sale_game',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			
			$data['uniacid']=$_SESSION['uniacid'];
			$data['merchid']=$_SESSION['onemerchid'];
			$data['createtime']=TIMESTAMP;
			$data['type']=2;
			pdo_insert('jy_scrm_sale_game',$data);
		}
		if($data['starttime'] >$data['endtime']){
			exi('开始时间必须小于结束时间！','error');
		}
		exi('保存活动成功！','','list');
	}	
	if($id){
		$item=pdo_get('jy_scrm_sale_game',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		$item['starttime'] =$item['starttime']?date('Y/m/d H:i',$item['starttime']):'';
		$item['endtime'] =$item['endtime']?date('Y/m/d H:i',$item['endtime']):'';
	}else{
		$item=array(
			'prize_sendtype'=>'0',
			'prize_type'=>'0',
			'get_sextype'=>'0',
			'get_areatype'=>'0',
			'red_type'=>'1',
			'red_nexttype'=>'1',
			'share_image'=>'../addons/jy_scrm/resource/images/redshare.png',
			'share_title'=>'独乐乐不如众乐乐，一起快来拆红包，领钱吧！',
			'share_desc'=>'好友，快来拆红包，领钱啦~~',
		);	
	}
	$item['red_set']=json_decode($item['red_set'],true);
	$item['poster_data']=$item['poster_data']?json_decode($item['poster_data'],true):array('data'=>$this->poster_reddatademo,'image'=>'../addons/jy_scrm/resource/images/redposter.png');
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'活动信息',
				'list'=>array(
					array('type'=>'string','title'=>'活动名称','read'=>$id,'name'=>'title','help'=>'（一旦发布，不可修改）'),
					array('type'=>'time','title'=>'活动开始时间','read'=>$id,'name'=>'starttime','help'=>'（一旦发布，只可更改结束时间）'),
					array('type'=>'time','title'=>'活动结束时间','name'=>'endtime','help'=>'（一旦发布，只可更改结束时间）'),
					array('type'=>'text','title'=>'活动说明','name'=>'desc','help'=>'请输入活动时间、活动说明、活动规则、客服电话等告知参与者的信息'),
				),
			),
			array(
				'title'=>'规则设置',
				'list'=>array(
					array('type'=>'unitstring','unit'=>'元','title'=>'裂变红包金额','name'=>'red_fee','help'=>''),
					array('type'=>'unitstring','unit'=>'个','title'=>'裂变红包个数','name'=>'red_num','help'=>''),
					array('type'=>'radio','title'=>'用户首次拆领类型','name'=>'red_type','options'=>'red_type','help'=>''),
					array('type'=>'string_two','title'=>'随机金额','title1'=>'最小金额','name1'=>'red_type0_min','unit1'=>'元','unit2'=>'元','title2'=>'最大金额','name2'=>'red_type0_max','show'=>array('red_type'=>0),'help'=>''),
					array('type'=>'unitstring','unit'=>'元','title'=>'固定金额','name'=>'red_type1_fee','show'=>array('red_type'=>1),'help'=>''),
					array('type'=>'string_two','title'=>'随机金额百分比','title1'=>'最小百分比','name1'=>'red_type2_min','unit1'=>'%','unit2'=>'%','title2'=>'最大百分比','name2'=>'red_type2_max','show'=>array('red_type'=>2),'help'=>''),
					array('type'=>'string','title'=>'裂变人数','name'=>'red_needsharenum','help'=>'邀请多少位好友可拆领红包！'),
					array('type'=>'string','title'=>'好友助力次数','name'=>'red_limitzulinum','help'=>'（不填写，则默认为不限制。一旦发布，不可修改）'),
					array('type'=>'radio','title'=>'好友拆领','name'=>'red_nexttype','options'=>'red_nexttype','help'=>'裂变不成功，余下红包金额数将不再发放。'),
					array('type'=>'string_two','title'=>'随机金额','title1'=>'最小金额','name1'=>'red_nexttype0_min','unit1'=>'元','unit2'=>'元','title2'=>'最大金额','name2'=>'red_nexttype0_max','show'=>array('red_nexttype'=>0),'help'=>''),
					array('type'=>'unitstring','unit'=>'元','title'=>'固定金额','name'=>'red_nexttype1_fee','show'=>array('red_nexttype'=>1),'help'=>''),
					array('type'=>'string','title'=>'余下红包发放规则','name'=>'red_lastsendtype','help'=>'裂变不成功，余下红包金额数将不再发放。'),
				),
			),
			array(
				'title'=>'高级设置',
				'list'=>array(
					array('type'=>'tip','title'=>'参与限制（不论是个人拆领红包，还是其好友拆领，无法满足下列要求，均拆领失败） ',),
					array('type'=>'radio','title'=>'性别参与 ','read'=>$id,'name'=>'get_sextype','options'=>'get_sextype','help'=>'（一旦发布，不可修改。以在本系统的性别数据为准）'),
					array('type'=>'radio','title'=>'地区参与','read'=>$id,'name'=>'get_areatype','options'=>'get_areatype','help'=>'（一旦发布，可参与的地区只可增加）'),
					array('name'=>'get_areas','title'=>'选择参与地区','read'=>$id,'type'=>'choose','show'=>array('get_areatype'=>1),'action'=>'jy_weishop/shop.peisong/choose/'),
					array('type'=>'choose','title'=>'打客户标签 ','name'=>'set_tag','action'=>'jy_scrm/member.get_tag/','help'=>'不论邀请人，还是助力者，只要参与该场活动，则自动打上选中的客户标签）'),
				),
			),
			array(
				'title'=>'活动海报',
				'list'=>array(
					array('type'=>'poster','title'=>'活动海报','name'=>'poster_data','datatypes'=>'datatypes','datademo'=>'datademo','datapushs'=>'datapushs'),
				),
			),
			array(
				'title'=>'活动欢迎语',
				'list'=>array(
					array('type'=>'tip','title'=>'引流设置 海报将展示对应带参数的二维码， 扫码后将引流到对应的企业成员 ',),
					array('type'=>'choose','title'=>'引流成员 ','name'=>'workerid','action'=>'jy_scrm/worker.get_worker/'),
					array('type'=>'text','title'=>'设置欢迎语','name'=>'welcome','help'=>'变量：[客户名称]'),
					array('type'=>'string','title'=>'分享标题','name'=>'share_title'),
					array('type'=>'image','title'=>'分享图片','name'=>'share_image'),
					array('type'=>'text','title'=>'分享描述','name'=>'share_desc'),
				),
			),
		)
	);
	$return['prize_sendtype']=array('活动期间','活动结束');
	$return['get_sextype']=array('不限制','仅男性参与','仅女性参与','未知');
	$return['get_areatype']=array('不限制','部分地区参与');
	$return['prize_type']=array('实物','红包');
	$return['red_type']=array('按随机金额','按固定金额','按随机金额百分比');
	$return['red_nexttype']=array('按随机金额','按固定金额');
	$return['datatypes']=$this->poster_datatypes;
	$return['datademo']=array('data'=>$this->poster_reddatademo,'image'=>'../addons/jy_scrm/resource/images/redposter.png');
	$return['datapushs']=$this->poster_datapushs;
	$return['htmls'][]=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_scrm_sale_game',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择会员！','error');
	}
	$post=get_postdata();
	if(isset($post['isblack'])){
		pdo_update('jy_scrm_sale_game',array('isblack'=>intval($post['isblack'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}